package com.pangolin.pangolinim.ui.activity;


import android.content.IntentFilter;
import android.widget.CompoundButton;

import com.kyleduo.switchbutton.SwitchButton;
import com.lqr.optionitemview.OptionItemView;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.app.BroadcastMsg;
import com.pangolin.pangolinim.net.NetFormat;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.SPUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.util.HashMap;
import java.util.Map;

public class SettingActivity extends BaseActivity {
    private UIContact me;
    private OptionItemView mOivExitApp;
    private OptionItemView mOivExitAcc;
    private OptionItemView mOivAbout;
    private SwitchButton mSbMsgNotify;
    private OptionItemView mOivAccountAndSafeSet;

    private boolean isSendThreadRunning = true;
    private boolean isRecvThreadRunning = true;

    @Override
    protected void init(){
        String strMe = getIntent().getStringExtra("param1");
        if(strMe != null){
            me = GsonUtil.fromJson(strMe , UIContact.class);
        }
    }

    @Override
    protected void initView(){
        mOivExitAcc = findViewById(R.id.oivExitAcc);
        mOivExitApp = findViewById(R.id.oivExitApp);
        mOivAbout = findViewById(R.id.oivAbout);
        mSbMsgNotify = findViewById(R.id.sbMsgNotify);
        mOivAccountAndSafeSet = findViewById(R.id.oivAccountAndSafeSet);

        setToolbarTitle(UIUtil.getString(R.string.setting));
    }

    @Override
    protected void onResume() {
        super.onResume();
        //发送获取系统设置请求
        Map<String, Object> brocastMap = new HashMap<String, Object>();
        brocastMap.put("type" , BroadcastMsg.DATA_UI_GETCONFIG);
        brocastMap.put("key" , "notify");
        BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
        BroadcastSender(bundleUtil.getBundle());
    }

    @Override
    protected void initListener(){
        mOivAccountAndSafeSet.setOnClickListener(v -> {
            jumpToActivity(AccAndSafeActivity.class, GsonUtil.toJson(me));
        });
        mOivAbout.setOnClickListener(v -> jumpToActivity(AboutActivity.class));
        mOivExitApp.setOnClickListener(v -> {
            /** 发送结束服务请求 */
            BundleUtil bundleUtilSend = new BundleUtil(BroadcastMsg.SIG_UI_STOP  , "");
            BroadcastSender(bundleUtilSend.getBundle());
        });
        mOivExitAcc.setOnClickListener(v -> {
            /** step1 - 发送退出登陆请求 */
            Map<String, Object> brocastMap = new HashMap<String, Object>();
            brocastMap.put("type" , BroadcastMsg.DATA_UI_LOGOUT);
            BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
            BroadcastSender(bundleUtil.getBundle());
        });
        mSbMsgNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Logger.d( "mSbMsgNotify, new state: " + (isChecked ? "on" : "off"));
                //发送获取系统设置请求
                Map<String, Object> brocastMap = new HashMap<String, Object>();
                brocastMap.put("type" , BroadcastMsg.DATA_UI_SETCONFIG);
                brocastMap.put("key" , "notify");
                brocastMap.put("value" , isChecked);
                BundleUtil bundleUtil = new BundleUtil(BroadcastMsg.SIG_UI_DATA , brocastMap , null);
                BroadcastSender(bundleUtil.getBundle());
            }
        });
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){
        if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_DATA) {
            if (bundleUtil.getData() != null) {
                Map<String, Object> resMap = bundleUtil.getData();
                Logger.d(resMap);
                if ((int) resMap.get("type") == BroadcastMsg.DATA_CORE_LOGOUT) {
                    if((int) resMap.get("result") == 0){
                        //清除缓存用户密码
                        SPUtil.remove(this , "uid");
                        SPUtil.remove(this , "passwd");
                        UIUtil.showToast("用户成功退出");
                        jumpToActivity(SplashActivity.class);
                        finish();
                    }else{
                        UIUtil.showToast(NetFormat.ResMsg((int) resMap.get("result")));
                    }
                }else if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_GETCONFIG){
                    boolean isNotify = (boolean) resMap.get("value");
                    mSbMsgNotify.setChecked(isNotify);
                }else if((int) resMap.get("type") == BroadcastMsg.DATA_CORE_SETCONFIG){
                    if((int) resMap.get("result") != 0){
                        UIUtil.showToast("修改配置失败");
                    }
                }
            }
        }else if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_THREADEND){
            UIUtil.showToast("核心线程成功退出");
            //关闭服务
            stopCoreService();
        }else if(bundleUtil.getType() == BroadcastMsg.SIG_CORE_SERVSTOPED){
            /** step3 - 退出所有UI */
            Logger.d("service stop");
            exitApp();
        }
    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_setting;
    }

}